package com.sunyur.tailor.channel.service.inner.findsource.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurListResult;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelListResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.findsource.model.*;
import com.sunyur.tailor.channel.service.inner.findsource.param.FindSourceInforParam;
import com.sunyur.tailor.channel.service.inner.findsource.param.FindSourceQuoteListParam;
import com.sunyur.tailor.channel.service.inner.findsource.param.FindSourceQuoteSearchParam;
import com.sunyur.tailor.channel.service.inner.findsource.service.SunyurFindSourceService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:寻源实现类
 * @author: wjh
 * @time: 2021/5/14 0014 10:24
 */
@Service("sunyurFindSourceService")
public class SunyurFindSourceServiceImpl implements SunyurFindSourceService {


    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurFindSourceServiceImpl.class);

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    /**
     * 获取项目详情（头）
     */
    @Value("${sunyur.api.findSourceProjectHead}")
    private String findSourceProjectHead;
    /**
     * 获取项目详情（行）
     */
    @Value("${sunyur.api.findSourceProjectItem}")
    private String findSourceProjectItem;

    /**
     * 获取定标详情（头）
     */
    @Value("${sunyur.api.findSourceFixedHead}")
    private String findSourceFixedHead;

    /**
     * 获取定标详情（行）
     */
    @Value("${sunyur.api.findSourceFixedItem}")
    private String findSourceFixedItem;

    /**
     * 获取报价单列表
     */
    @Value("${sunyur.api.findSourceQuoteList}")
    private String findSourceQuoteList;

    /**
     * 获获取报价详情（头）
     */
    @Value("${sunyur.api.findSourceQuoteHead}")
    private String findSourceQuoteHead;

    /**
     * 获取报价单详情（行）
     */
    @Value("${sunyur.api.findSourceQuoteItem}")
    private String findSourceQuoteItem;

    @Override
    public ChannelResult<FindSourceProjectInforHeadModel> searchProjectInforHead(FindSourceInforParam findSourceInforParam) throws Exception {

        LOGGER_INFO.info("SunyurFindSourceServiceImpl searchProjectInforHead param is {}", JSON.toJSONString(findSourceInforParam));
        if (ObjectUtils.isEmpty(findSourceInforParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SunyurFindSourceServiceImpl searchProjectInforHead param is null");
        }
        // 调用主线接口
        SunyurResult<FindSourceProjectInforHeadModel> sunyurResult = sunyurClient.execPost(findSourceProjectHead, JSON.toJSONString(findSourceInforParam), new TypeReference<SunyurResult<FindSourceProjectInforHeadModel>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelListResult<FindSourceProjectInforItemModel> searchProjectInforItems(FindSourceInforParam findSourceInforParam) throws Exception {
        LOGGER_INFO.info("SunyurFindSourceServiceImpl searchProjectInforItems param is {}", JSON.toJSONString(findSourceInforParam));
        if (ObjectUtils.isEmpty(findSourceInforParam)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SunyurFindSourceServiceImpl searchProjectInforItems param is null");
        }
        // 调用主线接口
        SunyurListResult<FindSourceProjectInforItemModel> sunyurResult = sunyurClient.execListPost(findSourceProjectItem, JSON.toJSONString(findSourceInforParam), new TypeReference<SunyurListResult<FindSourceProjectInforItemModel>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelListResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelResult<FindSourceFixedInforHeadModel> searchFixedInforHead(FindSourceInforParam findSourceInforParam) throws Exception {
        LOGGER_INFO.info("SunyurFindSourceServiceImpl searchFixedInforHead param is {}", JSON.toJSONString(findSourceInforParam));
        if (ObjectUtils.isEmpty(findSourceInforParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SunyurFindSourceServiceImpl searchFixedInforHead param is null");
        }
        // 调用主线接口
        SunyurResult<FindSourceFixedInforHeadModel> sunyurResult = sunyurClient.execPost(findSourceFixedHead, JSON.toJSONString(findSourceInforParam), new TypeReference<SunyurResult<FindSourceFixedInforHeadModel>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelListResult<FindSourceFixedInforItemModel> searchFixedInforItem(FindSourceInforParam findSourceInforParam) throws Exception {
        LOGGER_INFO.info("SunyurFindSourceServiceImpl searchFixedInforItem param is {}", JSON.toJSONString(findSourceInforParam));
        if (ObjectUtils.isEmpty(findSourceInforParam)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SunyurFindSourceServiceImpl searchFixedInforItem param is null");
        }
        // 调用主线接口
        SunyurListResult<FindSourceFixedInforItemModel> sunyurResult = sunyurClient.execListPost(findSourceFixedItem, JSON.toJSONString(findSourceInforParam), new TypeReference<SunyurListResult<FindSourceFixedInforItemModel>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelListResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelListResult<FindSourceQuoteListModel> searchQuoteList(FindSourceQuoteListParam findSourceQuoteListParam) throws Exception {
        LOGGER_INFO.info("SunyurFindSourceServiceImpl searchQuoteList param is {}", JSON.toJSONString(findSourceQuoteListParam));
        if (ObjectUtils.isEmpty(findSourceQuoteListParam)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SunyurFindSourceServiceImpl searchQuoteList param is null");
        }
        // 调用主线接口
        SunyurListResult<FindSourceQuoteListModel> sunyurResult = sunyurClient.execListPost(findSourceQuoteList, JSON.toJSONString(findSourceQuoteListParam), new TypeReference<SunyurListResult<FindSourceQuoteListModel>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelListResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelResult<FindSourceQuoteSearchHeadModel> searchQuoteHead(FindSourceQuoteSearchParam findSourceQuoteSearchParam) throws Exception {
        LOGGER_INFO.info("SunyurFindSourceServiceImpl searchQuoteHead param is {}", JSON.toJSONString(findSourceQuoteSearchParam));
        if (ObjectUtils.isEmpty(findSourceQuoteSearchParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SunyurFindSourceServiceImpl searchQuoteHead param is null");
        }
        // 调用主线接口
        SunyurResult<FindSourceQuoteSearchHeadModel> sunyurResult = sunyurClient.execPost(findSourceQuoteHead, JSON.toJSONString(findSourceQuoteSearchParam), new TypeReference<SunyurResult<FindSourceQuoteSearchHeadModel>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelListResult<FindSourceQuoteSearchItemModel> searchQuoteItem(FindSourceQuoteSearchParam findSourceQuoteSearchParam) throws Exception {
        LOGGER_INFO.info("SunyurFindSourceServiceImpl searchQuoteItem param is {}", JSON.toJSONString(findSourceQuoteSearchParam));
        if (ObjectUtils.isEmpty(findSourceQuoteSearchParam)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SunyurFindSourceServiceImpl searchQuoteItem param is null");
        }

        List<FindSourceQuoteSearchItemModel> itemModelList = new ArrayList<>();
        Integer totalPage;
        Integer page = 0;
        findSourceQuoteSearchParam.setPageSize(100);
        //循环调用
        do {
            findSourceQuoteSearchParam.setPage(++page);
            // 调用主线接口
            SunyurListResult<FindSourceQuoteSearchItemModel> sunyurResult = sunyurClient.execListPost(findSourceQuoteItem, JSON.toJSONString(findSourceQuoteSearchParam), new TypeReference<SunyurListResult<FindSourceQuoteSearchItemModel>>() {
            });
            // 主线报错
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
            totalPage = sunyurResult.getTotalPage();
            itemModelList.addAll(sunyurResult.getContent());
        } while (findSourceQuoteSearchParam.getPage() < totalPage);

        return ChannelListResultUtil.success(itemModelList);
    }

}
